<script setup lang="ts">

import router from "@/router";
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import {revisePassword} from "@/api/home/login/login";
import {ElMessage} from "element-plus";

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('Please input the age'))
    }
    setTimeout(() => {
        if (!Number.isInteger(value)) {
            callback(new Error('Please input digits'))
        } else {
            if (value < 18) {
                callback(new Error('Age must be greater than 18'))
            } else {
                callback()
            }
        }
    }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        if (ruleForm.checkPass !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass', () => null)
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请重新输入密码'))
    } else if (value !== ruleForm.pass) {
        callback(new Error("两次输入不匹配"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    checkPass: '',
    age: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
    age: [{ validator: checkAge, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            let user = JSON.parse(localStorage.getItem('userInformation'))
            let dataArr = {
                id: user.id,
                newPassword: ruleForm.pass,
                password: ruleForm.checkPass
            }
            console.log(dataArr.id)
            revisePassword(dataArr).then(res=>{
                if (res.code == 200) {
                    ElMessage({
                        message: '修改成功',
                        type: 'success',
                    })
                    router.push({path: '/login'})
                }
            })
            console.log('submit!')
        } else {
            console.log('error submit!')
            return false
        }
    })
}
</script>

<template>
<!--修改密码-->
  <div class="box">
      <div class="titTime" @click="router.go(-1)">
          <van-icon name="arrow-left"/>
          <span class="titleText">密码设置</span>
      </div>
      <div class="substance">
          <el-form
                  ref="ruleFormRef"
                  style="max-width: 100%"
                  :model="ruleForm"
                  status-icon
                  :rules="rules"
                  label-width="auto"
                  class="demo-ruleForm"
          >
              <el-form-item label="新的密码" prop="pass">
                  <el-input v-model="ruleForm.pass" type="password" autocomplete="off" placeholder="请输入密码" style="border: none; outline: none; box-shadow: none;" />
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                  <el-input
                          v-model="ruleForm.checkPass"
                          type="password"
                          autocomplete="off"
                          placeholder="请再次输入密码"
                          style="border: none; outline: none; box-shadow: none;"
                  />
              </el-form-item>
              <el-form-item class="uploadBtn">
                  <el-button type="primary" @click="submitForm(ruleFormRef)" style="width: 50vw; margin-top: 40%; margin-right: 10%;">
                      提交
                  </el-button>
              </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<style scoped>
* {
    margin: 0;
    padding: 0;
}

.box {
    padding: 8% 6%;

    .titTime {
        margin-bottom: 5%;
        font-size: 5vw;
    }
    .substance {
        padding: 0 5%;
    }
}
.uploadBtn {
    margin: auto;
    width: 60%;
}
>>> .el-form-item__content {
    justify-content: center;
}
>>> .el-form-item{
    flex-direction: column;
}
>>> .el-input__inner{
    border: none;
}

</style>